package com.zh.config.act

import android.os.Bundle
import android.widget.ImageView
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.alibaba.android.arouter.facade.annotation.Route
import com.zh.common.R
import com.zh.common.base.BaseActivity
import com.zh.common.databinding.ActivityImagePagerBinding
import com.zh.common.ktx.bindAdapter
import com.zh.common.ktx.clickSingleListener
import com.zh.common.ktx.loadUrl
import com.zh.config.ZjConfig

/**
 * 查看大图
 */
@Route(path = ZjConfig.Routes.LOOK_BIG_IMAGE)
class ImagePagerActivity : BaseActivity<ActivityImagePagerBinding>() {
    override val statusBarColor: Int
        get() = R.color.color_black
    override val navigationBarColor: Int
        get() = R.color.color_black

    private val imageUrls by lazy {
        intent.getStringArrayListExtra("imageUrls")
    }
    private val pos by lazy {
        intent.getIntExtra("pos", 0)
    }

    private val iAdapter by lazy {
        bindAdapter<String>(R.layout.item_big_image) { holder, item ->
            val ivImage = holder.getView<ImageView>(R.id.ivImage)
            ivImage.loadUrl(item)
        }.apply {
            setOnItemClickListener { _, _, _ ->
                finish()
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        overridePendingTransition(R.anim.slide_fade_in, R.anim.slide_fade_out)
        viewBinding.apply {
            viewPager.adapter = iAdapter
            iAdapter.setList(imageUrls)
            viewPager.setCurrentItem(pos, false)
            tvNumber.text = "${pos + 1}/${imageUrls?.size}"
            viewPager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    tvNumber.text = "${position + 1}/${imageUrls?.size}"
                }
            })
        }
    }

    override fun onPause() {
        super.onPause()
        overridePendingTransition(R.anim.slide_fade_in, R.anim.slide_fade_out)
    }
}